﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Entity;

namespace School
{
    public partial class _Default : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        protected string categories = string.Empty;
        protected string kjxName = string.Empty;
        protected string kjxArticleID = string.Empty;
        protected string kjxContent = string.Empty;

        protected string gjmyName = string.Empty;
        protected string gjmyArticleID = string.Empty;
        protected string gjmyContent = string.Empty;

        protected string scyxName = string.Empty;
        protected string scyxArticleID = string.Empty;
        protected string scyxContent = string.Empty;

        protected string gjswName = string.Empty;
        protected string gjswArticleID = string.Empty;
        protected string gjswContent = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            //通知公告
            var tzggList = (from data in db.Article
                            where data.SubMenu.Categories == 24 && data.IsChecked == 1 && data.IsEnabled == true && data.IsMainNews == true
                            orderby data.UpdateTime descending
                            select new
                            {
                                data.ArticleID,
                                data.SubMenu.ParentCategory,
                                data.SubMenu.Categories,
                                data.Title,
                                data.UpdateTime
                            });
            rep_tzgg.DataSource = tzggList;
            rep_tzgg.DataBind();

            //学院动态
            var xydtList = (from data in db.Article
                            where data.SubMenu.Categories == 23 && data.IsChecked == 1 && data.IsEnabled == true
                        orderby data.UpdateTime descending
                        select new
                        {
                            data.ArticleID,
                            data.SubMenu.ParentCategory,
                            data.SubMenu.Categories,
                            data.Title,
                            data.UpdateTime
                        }).Take(5);
            rep_xyxw.DataSource = xydtList;
            rep_xyxw.DataBind();

            //图片新闻
            var xydtimgList = (from data in db.Article
                               where data.SubMenu.Categories == 23 && data.IsMainNews == true && data.IsChecked == 1 && data.IsEnabled == true
                               orderby data.UpdateTime descending
                               select new
                               {
                                   data.ArticleID,
                                   data.SubMenu.ParentCategory,
                                   data.SubMenu.Categories,
                                   data.Title,
                                   data.ImagePath
                               }).Take(5);
            rep_xyxwImg.DataSource = xydtimgList;
            rep_xyxwImg.DataBind();

            //专业介绍
            /*var kjxData = (from major in db.Article
                           where major.SubMenu.Categories == 12 && major.Title == "会计学"
                           select new
                           {
                               major.ArticleID,
                               major.SubMenu.Categories,
                               major.Title,
                               major.Content
                           }).FirstOrDefault();
            if (kjxData != null)
            {
                categories = kjxData.Categories.ToString();
                kjxName = kjxData.Title;
                kjxArticleID = kjxData.ArticleID.ToString();
                kjxContent = Entity.CommonString.MySubString(Entity.CommonString.StripHTML(kjxData.Content), 20);
            }

            var gjmyData = (from major in db.Article
                           where major.SubMenu.Categories == 12 && major.Title == "国际贸易"
                           select new
                           {
                               major.ArticleID,
                               major.Title,
                               major.Content
                           }).FirstOrDefault();
            if (gjmyData != null)
            {
                gjmyName = gjmyData.Title;
                gjmyArticleID = gjmyData.ArticleID.ToString();
                gjmyContent = Entity.CommonString.MySubString(Entity.CommonString.StripHTML(gjmyData.Content), 20);
            }


            var scyxData = (from major in db.Article
                            where major.SubMenu.Categories == 12 && major.Title == "市场营销"
                            select new
                            {
                                major.ArticleID,
                                major.Title,
                                major.Content
                            }).FirstOrDefault();
            if (scyxData != null)
            {
                scyxName = scyxData.Title;
                scyxArticleID = scyxData.ArticleID.ToString();
                scyxContent = Entity.CommonString.MySubString(Entity.CommonString.StripHTML(scyxData.Content), 20);
            }

            var gjswData = (from major in db.Article
                            where major.SubMenu.Categories == 12 && major.Title == "国际商务"
                            select new
                            {
                                major.ArticleID,
                                major.Title,
                                major.Content
                            }).FirstOrDefault();
            if (gjswData != null)
            {
                gjswName = gjswData.Title;
                gjswArticleID = gjswData.ArticleID.ToString();
                gjswContent = Entity.CommonString.MySubString(Entity.CommonString.StripHTML(gjswData.Content), 20);
            }*/

            //学子风采
            /*var studentData=(from student in db.Article
                            where student.SubMenu.Categories == 17 && student.IsChecked==1
                            orderby student.UpdateTime descending
                            select new
                            {
                                student.ArticleID,
                                student.SubMenu.Categories,
                                student.Title,
                                student.UpdateTime
                            }).Take(4);
            rep_student.DataSource = studentData;
            rep_student.DataBind();

            //教学信息
            var teachInfoData = (from teach in db.DownloadFile
                                 where teach.Cagetory == 20
                                 orderby teach.UploadTime descending
                                 select new
                                 {
                                     teach.Title,
                                     teach.FIilePath
                                 }).Take(4);
            rep_fileDownload.DataSource = teachInfoData;
            rep_fileDownload.DataBind();*/

            //经管文化
            var cultureCategory = (from menu in db.SubMenu
                                   where menu.ParentCategory == 39
                                   orderby menu.Categories
                                   select new
                                    {
                                        menu.ParentCategory,
                                        menu.Categories,
                                        menu.Name
                                    });
            rep_culture.DataSource = cultureCategory;
            rep_culture.DataBind();
            
            //教学工作
            var teacheData = (from t in db.Article
                              where t.SubMenu.Categories == 56 && t.IsChecked == 1 && t.IsEnabled == true
                              orderby t.UpdateTime descending
                              select new
                              {
                                  t.SubMenu.ParentCategory,
                                  t.SubMenu.Categories,
                                  t.ArticleID,
                                  t.Title,
                                  t.UpdateTime
                              }).Take(5);
            rep_jxgz.DataSource = teacheData;
            rep_jxgz.DataBind();

            //科研工作
            var researchData = (from t in db.Article
                                where t.SubMenu.Categories == 6 && t.IsChecked == 1 && t.IsEnabled == true
                                orderby t.UpdateTime descending
                                select new
                                {
                                    t.SubMenu.ParentCategory,
                                    t.SubMenu.Categories,
                                    t.ArticleID,
                                    t.Content,
                                    t.UpdateTime
                                }).Take(5);
            rep_kygz.DataSource = researchData;
            rep_kygz.DataBind();

            //学生工作
            var studenData = (from t in db.Article
                              where t.SubMenu.Categories == 16 && t.IsChecked == 1 && t.IsEnabled == true
                              orderby t.UpdateTime descending
                              select new
                              {
                                  t.SubMenu.ParentCategory,
                                  t.SubMenu.Categories,
                                  t.ArticleID,
                                  t.Title,
                                  t.UpdateTime
                              }).Take(5);
            rep_xsgz.DataSource = studenData;
            rep_xsgz.DataBind();

            //党建工作
            var partData = (from t in db.Article
                            where t.SubMenu.Categories == 10 && t.IsChecked == 1 && t.IsEnabled == true
                            orderby t.UpdateTime descending
                            select new
                            {
                                t.SubMenu.ParentCategory,
                                t.SubMenu.Categories,
                                t.ArticleID,
                                t.Title,
                                t.UpdateTime
                            }).Take(5);
            rep_djgz.DataSource = partData;
            rep_djgz.DataBind();

            //广告
            var adData = (from ads in db.Ad
                          orderby ads.UploadTime descending
                          select ads);
            rep_ads.DataSource = adData;
            rep_ads.DataBind();

            //底部轮播图
            var playImgs = (from imgs in db.PlayImgs
                            orderby imgs.UploadTime descending
                            select new
                            {
                                imgs.ImgPath,
                                imgs.url
                            });
            rep_playImgs.DataSource = playImgs;
            rep_playImgs.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WebPages/Search.aspx?type=" + rbtn_FindItem.SelectedValue + "&key=" + search_box.Text.Trim());
        }

        protected void rep_culture_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                byte category = byte.Parse(DataBinder.Eval(e.Item.DataItem, "Categories").ToString());
                var article = (from a in db.Article
                               where a.SubMenu.Categories == category && a.IsChecked == 1
                               orderby a.UpdateTime descending
                               select new
                               {
                                   a.ArticleID,
                                   a.SubMenu.ParentCategory,
                                   a.SubMenu.Categories,
                                   a.Title,
                                   a.UpdateTime
                               }).Take(5);
                Repeater cultureItem = (Repeater)e.Item.FindControl("rep_CultureItem");
                cultureItem.DataSource = article;
                cultureItem.DataBind();
            }
        }

        protected void rep_ads_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hl = e.Item.FindControl("hl") as HyperLink;
                Ad ad = e.Item.DataItem as Ad;
                if (ad.IsInnerLink.Value)
                {
                    hl.NavigateUrl = "/WebPages/Ad/AdDetails.aspx?aid=" + ad.Aid.ToString();
                }
                else
                {
                    hl.NavigateUrl = ad.Url;
                }
            }
        }
    }
}
